<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/21
 * Time: 14:45
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\admin\controller\weixin;

use app\common\controller\AdminController;
use app\common\model\WeixinExpressConfig;
use EasyAdmin\annotation\ControllerAnnotation;
use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

/**
 * Class Config
 * @package app\admin\controller\weixin
 * @ControllerAnnotation(title="快递配置")
 */
class ExpressConfig extends AdminController
{
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\WeixinExpressConfig();

        $this->assign('getStatusList', $this->model->getStatusList());

    }

    public function add()
    {
    }

    public function edit($id)
    {
    }

    /**
     * @NodeAnotation (title="配置列表")
     * @return mixed
     * @throws Exception
     */
    public function index()
    {
        $config = (new WeixinExpressConfig())->getBaseById(1);
        $config['description'] = htmlspecialchars_decode($config['description']);
        $config['exemption'] = htmlspecialchars_decode($config['exemption']);
        return $this->fetch('', [
            "row" => $config
        ]);
    }

    /**
     * @NodeAnotation (title="修改配置")
     * @return void
     * @throws Exception
     */
    public function save()
    {
        $title = $this->request->param('title', '', 'trim');
        $description = $this->request->param('description');
        $exemption = $this->request->param('exemption');
        $money = $this->request->param('money');
        $price = $this->request->param('price');
        $number = $this->request->param('number',0,'intval');
        if (empty($title) || empty($description) || empty($exemption)) {
            $this->error("请输入完整");
        }
        $data = [
            "title" => $title,
            "description" => $description,
            "exemption" => $exemption,
            "money" => $money,
            "price" => $price,
            "number"=>$number
        ];
        //查询是否存在
        $row = $this->model->getBaseByIdInfo(1);
        if (!$row) {
            $result = $this->model->insertSaveById($data);
        } else {
            $result = $row->save($data);
        }
        if ($result) {
            $this->success("成功");
        } else {
            $this->error("失败");
        }

    }
}